<?php
declare (strict_types = 1);

namespace app\api\middleware;

use app\api\server\Token as TokenServer;
use think\Response;

class CheckToken
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next): Response
    {
        //第一步先取token
        $token = $request->header('token');
        if(empty($token)) {
            return json(['error_code'=>999,'未提供Token验证失败','data'=>''],400);
        }
        //jwt进行校验token
        //dump($token);
        $res = (new TokenServer())->checkToken($token);
        //dump($res);die();
        if ($res['status'] != 200 ){
            return json(['error_code'=>999,'msg'=>$res['info'],'data'=>''],400);
        }
        //dump($request);die();
        //$request->uid = $res['data']->uid;
        return $next($request);
    }
}
